//
//  JKMainNavigationController.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/2.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

class JKMainNavigationController: UINavigationController {
    
    public var showLeftVcBlock: (()->())?

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 9.0, *) {
            
            let bar = UINavigationBar.appearance(whenContainedInInstancesOf: [JKMainNavigationController.self])
            bar.tintColor = UIColor.white
            
            bar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 18)]
            
        } else {
            
            let bar = UINavigationBar.appearance()
            bar.tintColor = UIColor.white
            
            bar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 18)]
        }
        
//        interactivePopGestureRecognizer?.delegate = self
        
        self.navigationBar.isTranslucent = true
        
        navigationBar.setBackgroundImage(UIImage(named: "blank"), for: UIBarMetrics.default)
        navigationBar.shadowImage = UIImage()
        
        // 全局右划返回
//        interactivePopGestureRecognizer?.isEnabled = false
//        let target = self.interactivePopGestureRecognizer?.delegate
//        let pan = UIPanGestureRecognizer.init(target: target!, action: Selector(("handleNavigationTransition:")))
//        pan.delegate = self
//        pan.maximumNumberOfTouches = 1
//        interactivePopGestureRecognizer?.view?.addGestureRecognizer(pan)

        
        interactivePopGestureRecognizer?.isEnabled = false
        let popRecognizer = UIPanGestureRecognizer()
        popRecognizer.delegate = self
//        popRecognizer.maximumNumberOfTouches = 1
        interactivePopGestureRecognizer?.view?.addGestureRecognizer(popRecognizer)
        
        //#if USE_方案一
        //    _navT = [[NavigationInteractiveTransition alloc] initWithViewController:self];
        //    [popRecognizer addTarget:_navT action:@selector(handleControllerPop:)];
        //
        //#elif USE_方案二
        /**
         *  获取系统手势的target数组
         */
        let _targets = interactivePopGestureRecognizer?.value(forKey: "_targets") as! NSArray
        /**
         *  获取它的唯一对象，我们知道它是一个叫UIGestureRecognizerTarget的私有类，它有一个属性叫_target
         */
        let gestureRecognizerTarget = _targets.firstObject as AnyObject
        /**
         *  获取_target:_UINavigationInteractiveTransition，它有一个方法叫handleNavigationTransition:
         */
        let navigationInteractiveTransition = gestureRecognizerTarget.value(forKey: "_target")
        /**
         *  通过前面的打印，我们从控制台获取出来它的方法签名。
         */
        let handleTransition = NSSelectorFromString("handleNavigationTransition:")
        /**
         *  创建一个与系统一模一样的手势，我们只把它的类改为UIPanGestureRecognizer
         */
        popRecognizer.addTarget(navigationInteractiveTransition!, action: handleTransition)
        
        //#endif
    }
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

// MARK: - UIGestureRecognizerDelegate

extension JKMainNavigationController: UIGestureRecognizerDelegate {
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        /**
         *  这里有两个条件不允许手势执行，1、当前控制器为根控制器；2、如果这个push、pop动画正在执行（私有属性）
         */
        return viewControllers.count > 1 && !(self.value(forKey: "_isTransitioning") as! Bool)//![[self valueForKey:@"_isTransitioning"] boolValue];
    }
}
